package com.party.mobile.biz.mall;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.*;
import com.party.core.service.mall.*;
import com.party.core.service.mall.biz.StoreResourceBizService;
import com.party.core.utils.MyBeanUtils;
import com.party.mobile.web.dto.mall.output.MallCategoryOutput;
import com.party.mobile.web.dto.mall.output.MallSiftGoodsOutput;
import com.party.mobile.web.dto.mall.output.MallStoreOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;

/**
 * 商城店铺控制器
 * Created by yifeng
 *
 * @date 2018/4/10 0010
 * @time 上午 11:36
 */
@Service
public class MallStoreBizService {
    @Autowired
    private IMallStoreResourcesService mallStoreResourcesService;
    @Autowired
    private IMallStoreService mallStoreService;
    @Autowired
    private StoreResourceBizService storeResourceBizService;
    @Autowired
    private IMallGoodsService mallGoodsService;
    @Autowired
    private IMallBrandService mallBrandService;
    @Autowired
    private IMallShowSubjectService mallShowSubjectService;
    @Autowired
    private IMallCategoryService mallCategoryService;
    @Autowired
    private IMallShowSubjectTemplateService mallShowSubjectTemplateService;

    /**
     * 店铺主页
     *
     * @return
     * @throws Exception
     */
    public MallStoreOutput mainIndex() throws Exception {
        MallStore mallStore = mallStoreService.getUnique();
        if (mallStore == null) {
            throw new BusinessException(PartyCode.IS_NO_DATA, "店铺不存在");
        }
        MallStoreOutput mallStoreOutput = new MallStoreOutput();
        MyBeanUtils.copyBeanNotNull2Bean(mallStore, mallStoreOutput);

        String storeId = mallStore.getId();
        // 轮播图
        mallStoreOutput.setCarouselList(this.getCarousel(storeId));
        // 专题
        mallStoreOutput.setSubjectList(this.getSubjects());
        // 每日特惠
        mallStoreOutput.setDailyDealList(this.getGoods(storeId, MallStoreResources.MODEL_DAILY_DEAL, new Page(1, 2)));
        // 戈友推荐
        mallStoreOutput.setFriendList(this.getGoods(storeId, MallStoreResources.MODEL_FRIEND, null));
        // 品牌推荐
        mallStoreOutput.setBrandList(this.getBrands(storeId));
        // 分类商品导航
        mallStoreOutput.setCategoryList(this.getCategories(storeId));
        return mallStoreOutput;
    }

    /**
     * 轮播图
     *
     * @param storeId 店铺id
     * @return
     */
    public List<MallStoreOutput.CarouselOutput> getCarousel(String storeId) {
        List<MallStoreResources> resources = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_CAROUSEL);
        List<MallStoreOutput.CarouselOutput> outputs = LangUtils.transform(resources, new Function<MallStoreResources, MallStoreOutput.CarouselOutput>() {
            @Nullable
            @Override
            public MallStoreOutput.CarouselOutput apply(@Nullable MallStoreResources input) {
                MallStoreOutput.CarouselOutput output = new MallStoreOutput.CarouselOutput();
                output.setPic(input.getPic());
                String url = storeResourceBizService.appendLink(input);
                output.setUrl(url);
                return output;
            }
        });
        return outputs;
    }

    /**
     * 获取商品（每日特惠、戈友推荐）
     *
     * @param storeId 店铺id
     * @param model   模块
     * @param page
     * @return
     */
    public List<MallSiftGoodsOutput> getGoods(String storeId, Integer model, Page page) {
        List<MallStoreResources> resources = mallStoreResourcesService.findByStoreId(storeId, model, page);
        List<MallSiftGoodsOutput> outputs = LangUtils.transform(resources, new Function<MallStoreResources, MallSiftGoodsOutput>() {
            @Nullable
            @Override
            public MallSiftGoodsOutput apply(@Nullable MallStoreResources input) {
                MallSiftGoodsOutput output = new MallSiftGoodsOutput();
                String targetId = input.getTargetId();
                if (input.getType().equals(MallStoreResourcesType.GOODS.getCode())) {
                    MallGoods mallGoods = mallGoodsService.get(targetId);
                    output.setId(mallGoods.getId());
                    output.setOriginalPrice(mallGoods.getOriginalPrice());
                    output.setPic(mallGoods.getPic());
                    output.setName(mallGoods.getName());
                    output.setPrice(mallGoods.getPrice());
                    output.setType(mallGoods.getType());
                }
                output.setIsHot(false);
                return output;
            }
        });
        return outputs;
    }

    /**
     * 获取品牌
     *
     * @param storeId 店铺id
     * @return
     */
    public List<MallStoreOutput.BrandOutput> getBrands(String storeId) {
        List<MallStoreResources> resources = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_BRAND);
        List<MallStoreOutput.BrandOutput> outputs = LangUtils.transform(resources, new Function<MallStoreResources, MallStoreOutput.BrandOutput>() {

            @Nullable
            @Override
            public MallStoreOutput.BrandOutput apply(@Nullable MallStoreResources input) {
                MallStoreOutput.BrandOutput output = new MallStoreOutput.BrandOutput();
                if (input.getType().equals(MallStoreResourcesType.BRAND.getCode())) {
                    MallBrand mallGoodsBrand = mallBrandService.get(input.getTargetId());
                    output.setId(mallGoodsBrand.getId());
                    output.setName(mallGoodsBrand.getName());
                    output.setLogo(mallGoodsBrand.getLogo());
                }
                return output;
            }
        });
        return outputs;
    }

    /**
     * 专题
     *
     * @return
     */
    public List<MallStoreOutput.SubjectOutput> getSubjects() {
        // 专题分类
        MallShowSubjectTemplateQuery query = new MallShowSubjectTemplateQuery();
        query.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        List<MallShowSubjectTemplate> templates = mallShowSubjectTemplateService.listPage(query, new Page(1, 8));
        List<MallStoreOutput.SubjectOutput> outputs = LangUtils.transform(templates, template -> {
            MallStoreOutput.SubjectOutput output = new MallStoreOutput.SubjectOutput();
            output.setId(template.getId());
            output.setName(template.getName());
            output.setPic(template.getPic());
            return output;
        });

        return outputs;
    }

    /**
     * 获取分类
     *
     * @param storeId 店铺id
     * @return
     */
    public List<MallCategoryOutput> getCategories(String storeId) {
        List<MallStoreResources> resources = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_CATEGORY);
        Set<String> categoryIds = Sets.newHashSet();
        for (MallStoreResources db : resources) {
            categoryIds.add(db.getRemarks());
        }

        List<MallCategoryOutput> outputs = Lists.newArrayList();
        for (String categoryId : categoryIds) {
            MallCategoryOutput output = new MallCategoryOutput();
            MallCategory mallCategory = mallCategoryService.get(categoryId);
            output.setId(mallCategory.getId());
            output.setName(mallCategory.getName());
            output.setPic(mallCategory.getPic());

            List<MallSiftGoodsOutput> goodsOutputs = this.findByCategoryId(categoryId, storeId);
            output.setGoodsOutputs(goodsOutputs);
            outputs.add(output);
        }
        return outputs;
    }

    /**
     * 根据配置的商品对应的分类获取商品
     *
     * @param categoryId 分类编号
     * @return
     */
    public List<MallSiftGoodsOutput> findByCategoryId(String categoryId, String storeId) {
        MallStoreResources query = new MallStoreResources();
        query.setStoreId(storeId);
        query.setRemarks(categoryId);
        query.setModel(MallStoreResources.MODEL_CATEGORY);
        List<MallStoreResources> resources = mallStoreResourcesService.list(query);
        List<MallSiftGoodsOutput> outputs = LangUtils.transform(resources, new Function<MallStoreResources, MallSiftGoodsOutput>() {
            @Nullable
            @Override
            public MallSiftGoodsOutput apply(@Nullable MallStoreResources input) {
                MallSiftGoodsOutput output = new MallSiftGoodsOutput();
                String targetId = input.getTargetId();
                if (input.getType().equals(MallStoreResourcesType.GOODS.getCode())) {
                    MallGoods mallGoods = mallGoodsService.get(targetId);
                    output.setId(mallGoods.getId());
                    output.setOriginalPrice(mallGoods.getOriginalPrice());
                    output.setPic(mallGoods.getPic());
                    output.setName(mallGoods.getName());
                    output.setPrice(mallGoods.getPrice());
                    output.setType(mallGoods.getType());
                }
                return output;
            }
        });
        return outputs;
    }
}
